package com.glls.java2217.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.glls.java2217.common.LayuiPageVo;
import com.glls.java2217.mapper.StudentMapper;
import com.glls.java2217.pojo.Student;
import com.glls.java2217.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @date 2023-01-05
 * @desc
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public LayuiPageVo<Student> studentPageData(Map<String, Object> params) {

        //使用mybatis的分页插件
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);

        List<Student> students =  studentMapper.selectByParams(params);

        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        //把mybatis的分页数据 封装到 layui的数据结构中
        LayuiPageVo<Student> studentLayuiPageVo = new LayuiPageVo<>();

        studentLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        studentLayuiPageVo.setMsg("分页列表数据");
        studentLayuiPageVo.setCount(studentPageInfo.getTotal()); //总记录数
        studentLayuiPageVo.setData(studentPageInfo.getList());  // 分页数据

        return studentLayuiPageVo;
    }

    @Override
    public int deleteStudentById(Integer id) {
        return studentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<String> getAddress() {
        return studentMapper.getAddress();
    }
}
